/**
 * 
 */
package cn.antia.cwin.db;

import java.util.ResourceBundle;

import cn.antia.cep.common.AntiaLog;
import cn.antia.cwin.SysConf;
import cn.antia.db.SQLGene;

/**
 * @author sunkeei
 * @date Nov 14, 2007
 * @version 0.1
 * @time 
 */
public class DBConfig {
	private static ResourceBundle rb = ResourceBundle.getBundle("cn.antia.cep.cwin.db");
	
	/***
	 * 
	 * @param dbType
	 * @param rs 返回参数(字符串数组(第一个为长度，第二个为最小值，第三个为最大值))
	 * @return
	 */
	public static String getJavaType(String dbType,String[] rs){
		String value = null;
		try{
			String key = SysConf.SYSTEM_DB + "." + dbType.toUpperCase();
			value = rb.getString(key);
			String an,av;
			if(value != null && value.trim().length() > 0){
				value = value.trim();
				String[] vs = value.split(";");
				if(vs.length == 1){
					return vs[0];
				}else{
					value = vs[0].trim();
					for(int i = 1;i<vs.length;i++){
						String[] vs1 = vs[i].split(":");
						an = vs1[0];
						av = vs1[1];
						if(an.equals("len")){
							rs[0] = av;
						}else if(an.equals("min")){
							rs[1] = av;
						}else if(an.equals("max")){
							rs[2] = av;
						}
					}
				}
			}
		}catch(Exception e){
			AntiaLog.error("db type mapping config error: db type:" + dbType);
		}
		return value;
	}

    public static SQLGene getSqlGene() {
        isSupportDB();
        String c = rb.getString(SysConf.SYSTEM_DB + ".sqlgene");
        try {
            return (SQLGene)Class.forName(c).newInstance();
        } catch (Exception e) {
             System.err.println("错误的系统设置：找不到SQL生成器:" + c);
            System.err.println("After 5 seconds system will be shutdown...");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ee) {
                ee.printStackTrace();
            }
            System.exit(0);
            return null;
        }
    }
	public static CwinSQLGene getCwinSqlGene() {
        isSupportDB();
        String c = rb.getString(SysConf.SYSTEM_DB + ".cwinsqlgene");
        try {
            return (CwinSQLGene) Class.forName(c).newInstance();
        } catch (Exception e) {
            System.err.println("错误的系统设置：找不到SQL生成器:" + c);
            System.err.println("After 5 seconds system will be shutdown...");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ee) {
                ee.printStackTrace();
            }
            System.exit(0);
            return null;
        }
    }

    public static boolean isSupportDB() {
        String dbs = rb.getString("supportDB");
        String[] dbss = dbs.split(",");
        for (String db : dbss) {
            if (db.equalsIgnoreCase(SysConf.SYSTEM_DB)) {
                return true;
            }
        }
        System.err.println("错误的系统设置：暂时不支持的数据库:" + SysConf.SYSTEM_DB);
        System.err.println("After 5 seconds system will be shutdown...");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.exit(0);
        return false;
    }

    public static void main(String[] args){
		DBConfig map = new DBConfig();
		String[] res = new String[]{"","",""};
		String jt = map.getJavaType("TINYINT",res);
		System.out.println("jtype:" + jt);
		System.out.println("rs:" + res);
		String s = "char(980)";
		System.out.println(s.substring(s.indexOf("(") + 1,s.indexOf(")")));
	}
}
